using BackEnd.Actors;
using BackEnd.Service;
using Microsoft.OpenApi.Models;
using System.Text.Json;
using System.Reflection;

var builder = WebApplication.CreateBuilder(args);


var services = builder.Services;

// Add services to the container.
services.AddControllers().AddDapr();

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
services.AddEndpointsApiExplorer();

#region 注册 Swagger 服务
services.AddSwaggerGen(options =>
{
    string version = builder.Configuration["SwaggerDocumentInfo:Version"];
    options.SwaggerDoc(version, new OpenApiInfo
    {
        Title = builder.Configuration["SwaggerDocumentInfo:Title"],
        Version = version,
        Description = builder.Configuration["SwaggerDocumentInfo:Description"],
        TermsOfService = new Uri(builder.Configuration["SwaggerDocumentInfo:TermsOfService"]),
        Contact = new OpenApiContact
        {
            Name = builder.Configuration["SwaggerDocumentInfo:Contact:Name"],
            Email = builder.Configuration["SwaggerDocumentInfo:Contact:Email"],
            Url = new Uri(builder.Configuration["SwaggerDocumentInfo:Contact:Url"])
        },
        License = new OpenApiLicense
        {
            Name = builder.Configuration["SwaggerDocumentInfo:License:Name"],
            Url = new Uri(builder.Configuration["SwaggerDocumentInfo:License:Url"])
        }
    });
    options.OrderActionsBy(o => o.RelativePath);

    // 获取xml文件名
    var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
    // 获取xml文件路径
    var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
    // 添加控制器层注释，true 表示显示控制器注释
    options.IncludeXmlComments(xmlPath, true); 

});
#endregion


services.AddGrpc(); // 添加 Grpc

// 注册 actor
services.AddActors(options =>
{
    var jsonSerializerOptions = new JsonSerializerOptions()
    {
        PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
        PropertyNameCaseInsensitive = true
    };

    options.JsonSerializerOptions = jsonSerializerOptions;
    options.Actors.RegisterActor<WorkflowActor>();
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    #region 添加 Swagger 生成器 Swagger UI 中间件
    string version = builder.Configuration["SwaggerDocumentInfo:Version"];
    string title = builder.Configuration["SwaggerDocumentInfo:Title"];
    app.UseSwagger();
    app.UseSwaggerUI(options =>
    {
        options.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"{version}/{title}");

        // 路径设置，配置为空，表示直接在根域名（localhost://5010）访问该文件
        options.RoutePrefix = "swagger";
    });
    #endregion
}

app.UseAuthorization();

app.MapGrpcService<HelloService>();

//ӳ��Actor·��
app.MapActorsHandlers();

app.MapControllers();

//app.UseEndpoints(endpoints =>
//{
//    endpoints.MapControllers();
//    endpoints.MapGrpcService<HelloService>();
//});

app.Run();
